package com.imast.android.bys.ui.fragment;

import java.util.ArrayList;
import java.util.List;

import com.imast.android.bys.R;
import com.imast.android.bys.adapter.ProductoAdapter;
import com.imast.android.bys.io.model.Producto;
import com.imast.android.bys.task.SoapObjectTaskFragment;
import com.imast.android.bys.util.SoapClient;

import android.app.ProgressDialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class ProductoHelper implements OnItemClickListener{
	
	private static final String TAG_TASK_FRAGMENT = "taskSoap_fragment2";
	
	private SoapObjectTaskFragment mTaskFragment;
	
	private ProductoFragment fragment;
	private FragmentActivity mActivity;
	private ActionBar actionBar;
	
	private ListView lvProducto;
	private ProductoAdapter adapter;
	private ProgressDialog pDialog;
	private List<Producto> productoList = new ArrayList<Producto>();
	
	public ProductoFragment getFragment() {
		return fragment;
	}
	
	public FragmentActivity getActivity() {
		return mActivity;
	}

	public ActionBar getActionBar() {
		return actionBar;
	}
	
	public ListView getListViewProducto() {
		return lvProducto;
	}
	
	public SoapObjectTaskFragment getTaskFragment() {
		return mTaskFragment;
	}

	public void setTaskFragment(SoapObjectTaskFragment mTaskFragment) {
		this.mTaskFragment = mTaskFragment;
	}

	public ProductoHelper(ProductoFragment fragment, FragmentActivity mActivity, View rootView) {
		this.fragment = fragment;
		this.mActivity = mActivity;
		this.actionBar = ((ActionBarActivity)mActivity).getSupportActionBar();
		
		lvProducto = (ListView)rootView.findViewById(R.id.listProducto);
		lvProducto.setOnItemClickListener(this);
		
		initConfig();
	}
	
	public void onActivityCreated(){
		
		FragmentManager fm = getActivity().getSupportFragmentManager();
		mTaskFragment = (SoapObjectTaskFragment) fm.findFragmentByTag(TAG_TASK_FRAGMENT);

		if (mTaskFragment == null) {
			mTaskFragment = new SoapObjectTaskFragment();
			mTaskFragment.setTargetFragment(getFragment(), 0);
			fm.beginTransaction().add(getTaskFragment(), TAG_TASK_FRAGMENT).commit();
		}
		
	}
	
	private void initConfig(){
		adapter = new ProductoAdapter(getActivity().getApplicationContext(), productoList);
		getListViewProducto().setAdapter(adapter);
		
		getActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#1b1b1b")));
		
		getProductos("");
		
		/*JsonArrayRequest productoReq = new JsonArrayRequest(url, 
				new Response.Listener<JSONArray>() {

					@Override
					public void onResponse(JSONArray response) {
						hidePDialog();
						
						for (int i = 0; i < response.length(); i++) {
							try {
								
								JSONObject obj = response.getJSONObject(i);
								Producto producto = new Producto();
								producto.setTitulo(obj.getString("title"));
								producto.setThumbilUrl(obj.getString("image"));
								producto.setDescripcion(obj.getString("rating"));
								producto.setPrecio(obj.getDouble("releaseYear"));
								
								productoList.add(producto);
								
							} catch (Exception e) {
								e.printStackTrace();
							}
						}
						
						adapter.notifyDataSetChanged();						
					}
			
		}, new ErrorListener() {

			@Override
			public void onErrorResponse(VolleyError error) {
				hidePDialog();
			}
		});
		
		VolleySingleton.getInstance(getActivity().getApplicationContext()).addToRequestQueue(productoReq);*/
	}
	
	public void setAdapterChange(List<Producto> lstProducto){
		getListViewProducto().setAdapter(new ProductoAdapter(getActivity(), lstProducto));
		adapter.notifyDataSetChanged();
	}
	
	public void showDialog() {
		pDialog = new ProgressDialog(getActivity());
		pDialog.setMessage("Cargando...");
		pDialog.show();
	}
	
	public void hidePDialog() {
        if (pDialog != null) {
            pDialog.dismiss();
            pDialog = null;
        }
	}
	
	public void getProductos(String prodName){
		
		FragmentManager fm = getActivity().getSupportFragmentManager();
		mTaskFragment = (SoapObjectTaskFragment) fm.findFragmentByTag(TAG_TASK_FRAGMENT);

		String methodName = "BuscarProducto";
		String soapAction = "http://tempuri.org/IServiciosByS/BuscarProducto";
		SoapClient soapClient = new SoapClient(methodName, soapAction);
		soapClient.getRequest().addProperty("ProductoBusqueda", prodName);

		mTaskFragment = new SoapObjectTaskFragment();
		mTaskFragment.setTargetFragment(getFragment(), 0);
		fm.beginTransaction().add(mTaskFragment, TAG_TASK_FRAGMENT).commit();
		mTaskFragment.setSoapClient(soapClient);
		
		if (!mTaskFragment.isRunning()){
			mTaskFragment.start();
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		
		Producto prod = (Producto)getListViewProducto().getAdapter().getItem(position);
		
		DialogFragment newFragment = new ProductoDialogFragment(prod);
	    newFragment.show(getActivity().getSupportFragmentManager(), "ProductoDF");		
	}
}
